-- DungeonBlacksmithM
-- Created by chengb Mar/9/2015
-- 地牢工坊

module("DungeonBlacksmithM", package.seeall);

-- 计算工坊操作的探索点消耗
function calcSPCost(classId)
    return FormulaM.invoke("CALC_BLACKSMITH_COST_SP", ME.user, classId);
end

-------------------------
-- 判断该装备能否升级
-- @param classId   装备编号
function canEquipUpgraded(classId, upgradeStyle, isEquiped)
    local equipInfo = EquipM.query(classId);
    if not equipInfo then
        -- 装备不存在
        return false;
    end

    -- 是否允许该偏向装备升阶
    if table.indexOf(upgradeStyle, equipInfo["style"]) == -1 then
        return false;
    end

    -- 穿在身上的装备，只有物理偏向和魔法偏向才可以升阶
    if isEquiped == true then
        if equipInfo["style"] ~= EQUIP_STYLE_PHYSIC and
            equipInfo["style"] ~= EQUIP_STYLE_MAGIC then
            return false;
        end
    end

    -- 判断是否配置了下一级装备编号
    local nextClassId = equipInfo["next_class_id"];
    if type(nextClassId) ~= 'number' or
       nextClassId <= 0 then
       -- 未配置下一级装备编号
       return false;
    end

    if not EquipM.query(nextClassId) then
        -- 下一级装备不存在
        return false;
    end

    return true;
end

-------------------------
-- 升级装备
function upgradeEquip(gridPos, classId)
    if not DungeonM.checkDungeonAction() then
        -- 经检测，不允许执行地牢行为
        return false;
    end

    local grid = DungeonM.getGridByPos(gridPos);
    if not grid or grid.type ~= GRID_TYPE_BLACKSMITH then
        return false;
    end

    local elemId = grid.element;
    local elemData = DungeonM.queryElement(elemId, "dbase") or {};
    local upgradeStyle = elemData.upgrade_style or "normal";

    if true ~= canEquipUpgraded(classId, upgradeStyle, false) then
        -- 该装备不能升级
        return false;
    end

    -- 获取下一级装备编号
    local nextClassId = EquipM.query(classId, "next_class_id");

    if ItemM.getAmount(ME.user, classId) <= 0 then
        -- 装备数量不足
    	return false;
    end

    local needSP = calcSPCost(classId);
    if ME.user:queryAttrib("search_point") < needSP then
        -- 探索点不足
        return false;
    end

    -- 扣除探索点
    if not ME.user:costAttrib("search_point", needSP) then
        -- 探索点扣除失败
        return false;
    end

    -- 扣除装备
    if not ItemM.costAmount(ME.user, classId, 1) then
        -- 装备扣除失败了
        return false;
    end

    -- 替换成下一级装备
    ItemM.addAmount(ME.user, nextClassId, 1);

    -- 将格子设置为无效
    grid:changeState(GRID_STATE_DISABLE);

    -- 抛出装备升级事件
    EventMgr.fire(event.UPGRADE_EQUIP, { ["classId"] = classId, ["nextClassId"] = nextClassId, ["pos"] = gridPos, ["isSmith"] = 1});

    return true;
end

-- 升级穿戴的装备
-- @param classId    装备编号
-- @param gridPos    格子所在位置
function upgradeWearEquip(gridPos, classId)
    local grid = DungeonM.getGridByPos(gridPos);
    if not grid or grid.type ~= GRID_TYPE_BLACKSMITH then
        return false;
    end

    local elemId = grid.element;
    local elemData = DungeonM.queryElement(elemId, "dbase") or {};
    local upgradeStyle = elemData.upgrade_style or "normal";

    if true ~= canEquipUpgraded(classId, upgradeStyle, true) then
        -- 该装备不能升级
        return false;
    end

    local pos = 1;
    local equipType = EquipM.query(classId, "type");
    if not EquipM.getEquip(equipType, pos) then
        -- 该装备没有穿在身上
    	return false;
    end

    local needSP = calcSPCost(classId);
    if ME.user:queryAttrib("search_point") < needSP then
        -- 探索点不足
        return false;
    end

    -- 扣除探索点
    if not ME.user:costAttrib("search_point", needSP) then
        -- 探索点扣除失败
        return false;
    end

    -- 将格子设置为无效
    grid:changeState(GRID_STATE_DISABLE);

    -- 获取下一级装备编号
    local nextClassId = EquipM.query(classId, "next_class_id");

    -- 替换成下一级装备
    local equipments = ME.user.equipments;
    local equiped = equipments[equipType]
    equiped[pos] = nextClassId;

    -- 刷新附加属性
    PropM.refreshEffect(ME.user, "equip");

    EquipM.tryRecordSuitActive(nextClassId);

    -- 抛出装备升级事件
    EventMgr.fire(event.UPGRADE_EQUIP, { ["classId"] = classId, ["nextClassId"] = nextClassId, ["pos"] = gridPos, ["isSmith"] = 1});

    return true;
end